<?php
/*
 *      osguitable.php
 *
 *      Copyright 2010 Rakitin Vitaliy <virak@ukr.net>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

require_once($_SERVER['DOCUMENT_ROOT']."/classes/GUI/nwmsg.php");

class osguitable {

	protected $object;
	private $uuid;

	function __construct($uuid, $object) {
		$this->uuid = $uuid;
		$_SESSION[$uuid] = &$this;
		$this->object = &$object;
	}

	function get() {

		$out = "<table id='$this->uuid' align='center' style='border: solid black 1px;'>";
		$out .= "<tr>";
		foreach ($this->object->fields->getall() as $field) {
			$id = $field->uuid;
			$name = $field->name;
			if ($this->object->sortby == $id) {
				if ($this->object->direction == "ASC") {
					$out .= "<th style='border: solid black 1px;cursor:pointer;' onclick=message('$this->uuid','orderby','$id',0);>$name ASC</th>";
				} else {
					$out .= "<th style='border: solid black 1px;cursor:pointer;' onclick=message('$this->uuid','orderby','$id',0);>$name DESC</th>";
				}
			} else {
				$out .= "<th style='border: solid black 1px;cursor:pointer;' onclick=message('$this->uuid','orderby','$id',0);>$name</th>";
			}
		}
		$out .= "</tr>";

		foreach ($this->object->getitems() as $key=>$item) {
			if ($key != $this->object->select) {
				$out .= "<tr style='cursor:pointer;background-color:#FFFFFF;color:#000000;' onclick=message('$this->uuid','onclick','".$key."',0); ondblclick=message('$this->uuid','ondblclick','".$key."',0);>";
			} else {
				$out .= "<tr style='cursor:pointer;background-color:#B9C7D2;color:#000000;' onclick=message('$this->uuid','onclick','".$key."',0); ondblclick=message('$this->uuid','ondblclick','".$key."',0);>";
			}
			foreach($this->object->fields->getall() as $field) {
				$out .= "<td style='border: solid black 1px;'>";
				switch ($field->type) {
					case "float":
					case "varchar":// {$out .= $item[$field["id"]]; break;}
					case "date": {$out .= $item[$field->uuid]; break;}//{$out .= $item->fields->byuuid($field->uuid)->value; break;}
/*
					case "link": {
						$lufield = $this->object->items->byuuid($item->fields->byuuid($field->uuid)->value)->fields->byuuid($field->uuid);
						$out .= $GLOBALS["metadata"]->objects->byuuid($lufield->luobject)->items->byuuid($lufield->sense)->fields->byuuid($lufield->ludisplay)->value;
						break;
					}
*/
				}
				$out .= "</td>";
			}
			$out .= "</tr>";
		}
		$out .= "</table>";
		return $out;
	}
	
	function close() {
		unset($_SESSION[$this->uuid]);
	}

	function orderby($name) {
		if ($this->object->sortby == $name) {
			if ($this->object->direction == "ASC") {
				$this->object->direction = "DESC";
			} else {
				$this->object->direction = "ASC";
			}
		} else {
			$this->object->sortby = $name;
			$this->object->direction = "ASC";
		}
		$this->update();
	}

	function select($id) {
		$this->object->select = $id;
		$this->update();
	}

	function onclick($p1, $p2) {
		$this->select($p1);
	}

	function ondblclick($p1, $p2) {	}

	function update() {
		$answer = &$_SESSION['answer'];
        $answer[] = array('update',$this->uuid,$this->get());
	}
}
?>
